// pages/my_spell/my_spell.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        color_main: "",
        type: 0,
        //订单状态
        page: 1,
        //页码
        order: [],
        //订单
        search: "",
        //搜索关键词
        leader_enter: 0
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10),
            leader_enter: options.leader_enter || 0
        });
        wx.hideShareMenu();
        let that = this;
        that.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        this.getOrder();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function(e) {
        let that = this;
        console.log(e);
        let name = e.target.dataset.name;
        let icon = e.target.dataset.icon;
        let price = e.target.dataset.price;
        let proceed_id = e.target.dataset.id;
        console.log(proceed_id);
        return {
            title: "一起来拼团，" + name,
            imageUrl: icon,
            path: "/spell/pages/spell_detail/spell_detail?proceed_id=" + proceed_id
        };
    },
    //系统设置
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor
                });
                that.getOrder();
            }
        });
    },
    //获取订单
    getOrder: function() {
        let that = this;
        let page = that.data.page;
        let type = that.data.type;
        let search = that.data.search;
        common.request({
            endpoint: "Groupbuy/order/my_order",
            method: "post",
            data: {
                search: search,
                type: type,
                rows: 10,
                page: page,
                leader_enter: that.data.leader_enter
            },
            success: res => {
                var data = that.data.order;
                let order = page == 1 ? res.extra.rows : data.concat(res.extra.rows);
                if (res.extra.rows.length > 0) {
                    that.setData({
                        order: order
                    });
                    that.data.page = page + 1;
                }
            }
        });
    },
    //选择订单状态
    choose_type: function(e) {
        let that = this;
        let type = e.currentTarget.dataset.type;
        that.setData({
            page: 1,
            type: type,
            search: "",
            order: []
        });
        that.getOrder();
    },
    getValue: function(e) {
        var that = this;
        var search = e.detail.value;
        that.setData({
            search: search
        });
    },
    search: function() {
        let that = this;
        that.setData({
            page: 1,
            order: []
        });
        that.getOrder();
    },
    //立即支付
    pay: function(e) {
        var trade_no = e.currentTarget.dataset.ddh;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "order/pay",
            method: "post",
            data: {
                openid: openid,
                order_num: trade_no
            },
            success: res => {
                console.log(res);
                var order_no = res.extra.order_no;
                wx.requestPayment({
                    timeStamp: res.extra.timeStamp,
                    nonceStr: res.extra.nonceStr,
                    package: res.extra.package,
                    signType: res.extra.signType,
                    paySign: res.extra.paySign,
                    success: function(res) {
                        wx.showToast({
                            title: "支付成功",
                            icon: "success",
                            mask: true,
                            duration: 3e3,
                            success: function(res) {
                                console.log("支付成功");
                                wx.showToast({
                                    title: "购买成功！",
                                    icon: "success",
                                    success: function() {
                                        common.request({
                                            endpoint: "Order/common/sync-notify",
                                            method: "post",
                                            data: {
                                                order_no: order_no
                                            },
                                            success: res => {
                                                setTimeout(function() {
                                                    wx.redirectTo({
                                                        url: "/pages/my_spell/my_spell"
                                                    });
                                                }, 500);
                                            },
                                            fail(err) {
                                                wx.showModal({
                                                    title: "提示",
                                                    content: err.message
                                                });
                                            }
                                        });
                                        wx.showToast({
                                            title: "支付成功",
                                            icon: "success"
                                        });
                                    }
                                });
                            }
                        });
                    },
                    fail: function(res) {
                        wx.redirectTo({
                            url: "/pages/my_spell/my_spell"
                        });
                    },
                    complete: function(res) {
                        console.log("complete");
                    }
                });
            },
            fail: function(res) {
                wx.showModal({
                    title: "提示",
                    content: res.message
                });
            }
        });
    },
    //取消订单
    cancel_order: function(e) {
        var that = this;
        var trade_no = e.currentTarget.dataset.ddh;
        common.request({
            endpoint: "Order/orderinfo/cancel_order",
            method: "post",
            data: {
                trade_no: trade_no
            },
            success: res => {
                wx.redirectTo({
                    url: "/pages/my_spell/my_spell"
                });
            }
        });
    }
});